//
//  SetSecurityCodeViewController.m
//  Yihubang
//
//  Created by hyu on 16/8/1.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "LoginSecurityCodeVC.h"

@interface LoginSecurityCodeVC ()
@property (weak, nonatomic) IBOutlet UITextField * nSecurityCode;
@property (weak, nonatomic) IBOutlet UITextField * SecurityCodeCopy;
@property (weak, nonatomic) IBOutlet UIImageView *correctImg;
//@property (nonatomic, strong, readwrite) RACSignal *validcorrectSignal;
@end

@implementation LoginSecurityCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"安全密码设置";
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveSecurityCode:)] animated:TRUE];
    self.nSecurityCode.maxLenght=4;
    self.nSecurityCode.secureTextEntry=YES;
    self.SecurityCodeCopy.maxLenght=4;
    self.SecurityCodeCopy.secureTextEntry=YES;
    RACSignal * nSecurityCodeSignal = [self.nSecurityCode.rac_textSignal
                                       map:^id(NSString *text) {
                                           return text;
                                       }];
    RACSignal * SecurityCodeCopySignal =[self.SecurityCodeCopy.rac_textSignal
                                         map:^id(NSString *text) {
                                             return text;
                                         }];
    RACSignal * validLoginSignal = [RACSignal
                              combineLatest:@[ nSecurityCodeSignal, SecurityCodeCopySignal ]
                              reduce:^(NSString *username, NSString *password) {
                                  return @(username.length ==4 && [password isEqual:username]);
                              }];
    [validLoginSignal subscribeNext:^(NSNumber*signupActive){
        self.navigationItem.rightBarButtonItem.enabled =[signupActive boolValue];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
     self.navigationItem.leftBarButtonItem =nil;
    [self.navigationItem setHidesBackButton:YES];
}
-(void)saveSecurityCode :(id)button {
    [[[HttpManagerCenter sharedHttpManager] setSecurityCode:self.nSecurityCode.text resultClass:nil] subscribeNext:^(BaseModel *model){
        if(model.code==200){
            [self hiddenHUDWithString:@"设置成功" error:NO];
            [self.userClient updateSecurityCode:self.nSecurityCode.text];
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }else{
            [self hiddenHUDWithString:model.message error:NO];
        }
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
